﻿using System.Collections.Generic;

namespace Core.Variants
{
    public class VSortSet
    {
        //GameName:Id
        private readonly Dictionary<string, SortedSet<long>> name2IdDict = new();
        //Id:GameName
        private readonly Dictionary<long, string> id2NameDict = new();

        public int Count => id2NameDict.Count;

        public void Push(string game_name, long id)
        {
            if (string.IsNullOrWhiteSpace(game_name))
            {
                return;
            }

            if (name2IdDict.ContainsKey(game_name) == false)
            {
                name2IdDict[game_name] = new SortedSet<long>();
            }

            if (name2IdDict.ContainsKey("") == false)
            {
                name2IdDict[""] = new SortedSet<long>();
            }

            id2NameDict[id] = game_name;

            name2IdDict[""].Add(id);
            name2IdDict[game_name].Add(id);
        }

        public long Pop(string game_name = "")
        {
            if (string.IsNullOrWhiteSpace(game_name))
            {
                var set = name2IdDict[""];
                var id = set.Min;
                var name = id2NameDict[id];

                set.Remove(id);
                name2IdDict[name].Remove(id);
                id2NameDict.Remove(id);

                return id;
            }
            else
            {
                var set = name2IdDict[game_name];
                var id = set.Min;

                set.Remove(id);
                name2IdDict[""].Remove(id);
                id2NameDict.Remove(id);

                return id;
            }
        }
    }
}
